package com.aabte.permissionframeworklearn.permission.service;

import com.aabte.permissionframeworklearn.permission.beans.PageQuery;
import com.aabte.permissionframeworklearn.permission.beans.PageResult;
import com.aabte.permissionframeworklearn.permission.common.RequestHolder;
import com.aabte.permissionframeworklearn.permission.exception.ParamException;
import com.aabte.permissionframeworklearn.permission.mapper.SysAclMapper;
import com.aabte.permissionframeworklearn.permission.model.SysAcl;
import com.aabte.permissionframeworklearn.permission.param.AclParam;
import com.aabte.permissionframeworklearn.permission.util.BeanValidator;
import com.aabte.permissionframeworklearn.permission.util.IpUtil;
import com.google.common.base.Preconditions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author Daniel
 */
@Service
public class SysAclService {

    @Autowired
    private SysAclMapper sysAclMapper;

    @Autowired
    private SysLogService sysLogService;

    public SysAcl save(AclParam param) {
        BeanValidator.check(param);

        if (checkExist(param.getAclModuleId(), param.getName(), param.getId())) {
            throw new ParamException("当前权限模块下已存在相同名称的权限点！");
        }

        SysAcl sysAcl = SysAcl.builder()
                .name(param.getName())
                .aclModuleId(param.getAclModuleId())
                .type(param.getType())
                .seq(param.getSeq())
                .url(param.getUrl())
                .status(param.getStatus())
                .remark(param.getRemark())
                .code(generateCode())
                .operator(RequestHolder.getCurrentUser().getUsername())
                .operateIp(IpUtil.getRemoteIpAddress(RequestHolder.getCurrentRequest()))
                .operateTime(new Date())
                .build();

        sysAclMapper.insertSelective(sysAcl);
        sysLogService.saveAclLog(null, sysAcl);
        return sysAcl;
    }

    public SysAcl update(AclParam param) {
        BeanValidator.check(param);

        if (checkExist(param.getAclModuleId(), param.getName(), param.getId())) {
            throw new ParamException("当前权限模块下已存在相同名称的权限点！");
        }

        SysAcl before = sysAclMapper.selectByPrimaryKey(param.getId());
        Preconditions.checkNotNull(before, "待更新的权限点不存在！");

        SysAcl after = SysAcl.builder()
                .id(param.getId())
                .name(param.getName())
                .aclModuleId(param.getAclModuleId())
                .type(param.getType())
                .seq(param.getSeq())
                .url(param.getUrl())
                .status(param.getStatus())
                .remark(param.getRemark())
                .operator(RequestHolder.getCurrentUser().getUsername())
                .operateIp(IpUtil.getRemoteIpAddress(RequestHolder.getCurrentRequest()))
                .operateTime(new Date())
                .build();

        sysAclMapper.updateByPrimaryKeySelective(after);
        sysLogService.saveAclLog(before, after);
        return after;
    }

    public PageResult<SysAcl> getPageByAclModuleId(Long aclModuleId, PageQuery pageQuery) {
        BeanValidator.check(pageQuery);

        long count = sysAclMapper.countByAclModuleId(aclModuleId);
        if (count > 0) {
            List<SysAcl> acls = sysAclMapper.getPageByAclModuleId(aclModuleId, pageQuery);
            return PageResult.<SysAcl>builder().data(acls).total(count).build();
        }
        return PageResult.<SysAcl>builder().build();
    }

    private boolean checkExist(Long aclModuleId, String name, Long id) {
        return sysAclMapper.countByNameAndAclModuleId(aclModuleId, name, id) > 0;
    }

    private String generateCode() {
        return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + "_" + (int) (Math.random() * 100);
    }


}
